﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Community.aspx.cs" Inherits="OpenRAMS.Community.Community" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html />

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Community Admin</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <meta http-equiv="refresh" content="180" />
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" class="tableDefault">
    <tr>
    <td align="center"><div class="tableTitle">Community Staff Portal</div>
    <div class="navBar">
    <a href="../Staff/Staff.aspx" class="navLink" target="_parent">Home</a>
    <a href="new.aspx" class="navLink" target="_parent">New Booking</a>
    <a href="newClient.aspx" class="navLink" target="_parent">New Client</a>
    <a href="old.aspx" class="navLink" target="_parent">Archives</a>
    <a href="../Staff/logout.aspx" class="navLink">Logout</a>
    </div>
    </td>
    </tr>
    <tr>
    <td>
    <marquee behavior="scroll" direction="left" class="newsFlash">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/newsflash/community.txt"));
     %>    
     </marquee>
    </td>
    </tr>
    <tr>
    <td>
    <table width="100%" class="tableDefault">
    <tr>
    <td align="center"><div class="tableTitle">Pending Bookings</div></td>
    </tr>
    <tr>
    <td><div class="tableBody">
        <table width="100%">
        <tr><td>
        <%
            Bookings bookingList = new Bookings();
            bookingList.Populate("wontExist","bitApproved='false' AND bitActive='true'");
            List<Booking> bookList = bookingList.getBookingList();

            if (bookList.Count() > 1)
            {
                //arrange bookings so most recently created ticket appears first
                bookList.Sort(delegate(Booking a, Booking b)
                {
                    int x = a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                    if (x != 0) return -x;  //just make this positive to reverse the order ;)
                    else return a.createdTimeStamp.CompareTo(b.createdTimeStamp);
                });
            }
            if(bookList.Count()>0)
            {
                
                int loopCount = 0;

                Response.Write("<tr class=\"listTop\"><td><b>ID</b></td><td><b>Title</b></td><td><b>Description</b></td><td><b>Location</b></td><td><b>Start</b></td><td><b>End</b></td><td><b>Requester</b></td><td><b>Client</b></td><td><b>Created Date</b></td><td><b>Approve?</b></td></tr>");
                Response.Write("<tr><td colspan=\"10\"><hr /></td></tr>");

                foreach (Booking bk in bookList)
                {

                    Boolean result = false; //used later to set row colour & define clash link
                    int clashID = -1;

                    //Load room requests for this booking
                    RoomRequests bookingRoomRequests = new RoomRequests();
                    bookingRoomRequests.Populate(bk.bookID);
                    List<RoomRequest> roomRequests = bookingRoomRequests.getRoomRequests();

                    //for each of those rooms...
                    foreach (RoomRequest request in roomRequests)
                    {
                        //grab other active & approved bookings ready for comparison
                        Bookings testBookingList = new Bookings();
                        testBookingList.Populate("wontExist", "bitActive='true' AND bitApproved='true'");  //grabs active & approved bookings
                        List<Booking> testBookings = testBookingList.getBookingList();
                        
                        //Check if they request access to the same room.
                        foreach (Booking testBooking in testBookings)
                        {
                            RoomRequests testRequestList = new RoomRequests();
                            testRequestList.Populate(testBooking.bookID);
                            List<RoomRequest> testRequests = testRequestList.getRoomRequests();

                            foreach (RoomRequest testRequest in testRequests)
                            {
                                if (testRequest.RoomName == request.RoomName)
                                {
                                    //now we know both bookings are requesting access to the same room, we can now perform tests on them both
                                    //I have used 4 separate ifs for comment & readability purposes only
                                    
                                    //Checks booking doesn't clash with a larger existing booking
                                    if (testBooking.bookStart < bk.bookStart && testBooking.bookEnd > bk.bookEnd)
                                    {
                                        result = true;
                                        clashID = testBooking.bookID;
                                        break;
                                    }
                                    
                                    //Checks booking doesn't clash with a smaller existing booking (or bookings of same duration occurring @ same size)
                                    if (testBooking.bookEnd <= bk.bookEnd && testBooking.bookStart >= bk.bookStart)
                                    {
                                        result = true;
                                        clashID = testBooking.bookID;
                                        break;
                                    }
                                    
                                    //Check if test booking starts in the middle of this one
                                    if (testBooking.bookStart <= bk.bookEnd && testBooking.bookEnd >= bk.bookEnd)
                                    {
                                        result = true;
                                        clashID = testBooking.bookID;
                                        break;
                                    }
                                    
                                    //Check if test booking ends in the middle of this one
                                    if (testBooking.bookEnd <= bk.bookEnd && testBooking.bookEnd >= bk.bookStart)
                                    {
                                        result = true;
                                        clashID = testBooking.bookID;
                                        break;
                                    }
                                    
                                    //If the test booking has gotten this far, there is no clash
                                    
                                }
                            }
                            //We already know there's a clash by this point, so need to break out of all but main booking loop
                            if (result == true)
                            {
                                break;
                            }
                        }
                        if (result == true)
                        {
                            break;
                        }  
                    }
                    

                    if (result)
                    {
                        Response.Write("<tr class=\"badBooking\">");
                    }
                    else
                    {
                        Response.Write("<tr class=\"goodBooking\">");
                    }

                    Response.Write("<td>");
                    Response.Write(bk.bookID + "</td>");
                    Response.Write("<td>");
                    Response.Write(bk.bookTitle);
                    Response.Write("</td><td>");
                    Response.Write(bk.bookDesc);
                    Response.Write("</td><td>");

                    int commaCheck = 0;
                    foreach (RoomRequest request in roomRequests)
                    {
                        Response.Write(request.RoomName);
                        if (commaCheck < roomRequests.Count() - 1)
                        {
                            Response.Write(", ");
                        }
                    }
                    Response.Write("</td><td>");
                    Response.Write(bk.bookStart.ToString("ddd d MMM yyyy HH:mm"));
                    Response.Write("</td><td>");
                    Response.Write(bk.bookEnd.ToString("ddd d MMM yyyy HH:mm"));
                    Response.Write("</td><td>");
                    
                    UserRecord niceCreatorName = new UserRecord();
                    niceCreatorName.Load(bk.bookCreator);
                    Response.Write("<a href=\"mailto:" + niceCreatorName.userEmail + "\">");
                    Response.Write(niceCreatorName.userFirstName + " " + niceCreatorName.userSecondName);
                    Response.Write("</a>");
                    
                    Response.Write("</td><td>");
                    Response.Write(bk.bookClient);
                    Response.Write("</td><td>");
                    Response.Write(bk.createdTimeStamp.ToString("ddd d MMM yyyy HH:mm"));
                    Response.Write("</td><td>");

                    //Don't let the user 'approve' a booking if it clashes with another one
                    if (!result)
                    {
                        Response.Write("<a href=\"approve.aspx?bID=" + bk.bookID + "\">Approve</a> ");
                    }
                    else
                    {
                        Response.Write("<a href=\"edit.aspx?bID=" + clashID + "\">View Clash</a> ");
                    }
                                         
                    Response.Write("<a href=\"reject.aspx?bID=" + bk.bookID + "\">Reject</a>");
                    Response.Write("</td></tr>");
                    if(loopCount < bookList.Count() - 1)
                    {
                        Response.Write("<tr><td colspan=\"10\"><hr /></td></tr>");
                    }
                    loopCount++;
                }                
                
            }
            else
            {
                Response.Write("<div align=\"center\"><i>There are no pending bookings.</i></div>");
            }
            
         %>        
        </td></tr>
        </table>
    </div>
    </td>

    </tr>

    </table>
        
    
    </td>
    </tr>
    <tr>
    <td width="100%">
    <table width="100%" class="tableDefault">
        <tr><td align="center" class="tableTitle">Active Bookings</td></tr>
        <tr><td class="tableBody">
        <table width="100%">
        <tr class="listTop">
        <td><b>ID</b></td>
        <td><b>Title</b></td>
        <td><b>Description</b></td>
        <td><b>Location(s)</b></td>
        <td><b>Start</b></td>
        <td><b>End</b></td>
        <td><b>Resource(s)</b></td>
        <td><b>Assigned to</b></td>
        <td><b>Client</b></td>
        <td><b>Requester</b></td>
        
        <td><b>Options</b></td>
        </tr>
        <tr>
        <td colspan="11"><hr /></td>
        </tr>
        <%
          //for now, load all active bookings
            Bookings newBookingList = new Bookings();
            newBookingList.Populate("wontExist", "bitActive='true' AND bitApproved='true'");
            List<Booking> bookings = newBookingList.getBookingList();

            if (bookings.Count() > 0)
            {

                int loopCount = 0;
                
                //Order bookings by start date/time

                //arrange bookings so most recently created ticket appears first
                bookings.Sort(delegate(Booking a, Booking b)
                {
                    int x = a.bookStart.CompareTo(b.bookStart);
                    if (x != 0) return x;  //just make this positive to reverse the order ;)
                    else return a.bookStart.CompareTo(b.bookStart);
                });
                
                foreach (Booking booking in bookings)
                {
                    //This seems like a lot of work to just get a row colour
                    //however, the user records are recycled later to output friendly usernames
                    UserRecord assignee = new UserRecord();
                    assignee.Load(booking.bookUserAssigned);
                    UserRecord creator = new UserRecord();
                    creator.Load(booking.bookCreator);
                    ClientRecord client = new ClientRecord();
                    client.Load(booking.bookClient);
                    
                    //order of precedence:
                    //>Booking colour
                    //>Client colour
                    //>Assignee/Requester colour (if assignee is different to requester, assignee's colour is used)                    
                    //although, if booking date is passed gets grey treatment
                    if (booking.bookEnd < DateTime.Now)
                    {
                        Response.Write("<tr style=\"background-color:#CCCC99;\">");
                    }
                    else
                    {

                        if (booking.bookColour != "#FFFFFF")
                        {
                            Response.Write("<tr style=\"background-color:" + booking.bookColour + ";\">");
                        }
                        else
                        {
                            if (client.clientColour != "#FFFFFF")
                            {
                                Response.Write("<tr bgcolor=\"" + client.clientColour + "\">");
                            }
                            else
                            {
                                if (assignee.userName != creator.userName)
                                {
                                    Response.Write("<tr bgcolor=\"" + assignee.colourCode + "\">"); //ensures doesn't falsely flag creator's colour once reassigned
                                }
                                else
                                {
                                    Response.Write("<tr bgcolor=\"" + creator.colourCode + "\">");  //if this is still white, it stays white
                                }
                            }

                        }
                    }
                    
                    
                    Response.Write("<td>" + booking.bookID + "</td>");
                    Response.Write("<td>" + booking.bookTitle + "</td>");
                    Response.Write("<td>" + booking.bookDesc + "</td>");
                    
                    //Grab rooms associated with booking
                    RoomRequests roomRequestList = new RoomRequests();
                    roomRequestList.Populate(booking.bookID);
                    List<RoomRequest> roomRequests = roomRequestList.getRoomRequests();

                    Response.Write("<td>");
                    if (roomRequests.Count() > 0)
                    {
                        int aLoopCount = 0;
                        foreach (RoomRequest roomRequest in roomRequests)
                        {
                            Response.Write(roomRequest.RoomName);
                            if (aLoopCount < roomRequests.Count() - 1)
                            {
                                Response.Write(", ");
                            }
                            aLoopCount++;
                        }
                    }
                    else
                    {
                        Response.Write("N/A");
                    }
                    Response.Write("</td>");

                    Response.Write("<td>" + booking.bookStart.ToString("ddd dd MMM yyyy HH:mm") + "</td>");
                    Response.Write("<td>" + booking.bookEnd.ToString("ddd dd MMM yyyy HH:mm") + "</td>");
                    
                    //Grab all resources associated with this booking
                    ResourceRequests resourceRequestList = new ResourceRequests();
                    resourceRequestList.Populate(booking.bookID);
                    List<ResourceRequest> resourceRequests = resourceRequestList.getResourceRequests();

                    Response.Write("<td>");
                    if (resourceRequests.Count() > 0)
                    {
                        int bLoopCount = 0;
                        foreach (ResourceRequest resourceRequest in resourceRequests)
                        {
                            Response.Write(resourceRequest.ResName);
                            if (bLoopCount < resourceRequests.Count() - 1)
                            {
                                Response.Write(", ");
                            }
                            bLoopCount++;
                        } 
                    }
                    else
                    {
                        Response.Write("N/A");
                    }
                    Response.Write("</td>");

                    Response.Write("<td>" + assignee.userFirstName + " " + assignee.userSecondName + "</td>");
                    Response.Write("<td><a href=\"mailto:" + client.clientAddEmail + "\">" + client.clientName + "</a> ");

                    if (client.clientName != "Internal")
                    {
                        Response.Write("<a href=\"newClient.aspx?cID=\'");
                        String urlSetup = client.clientName.Replace(" ", "%20");
                        Response.Write(urlSetup + "\'\">Edit</td>");
                    }
                    Response.Write("<td>" + creator.userFirstName + " " + creator.userSecondName + "</td>");

                    Response.Write("<td>");
                    Response.Write("<a href=\"edit.aspx?bID=" + booking.bookID + "\">Edit</a> ");
                    Response.Write("<a href=\"finish.aspx?bID=" + booking.bookID + "\">Finish</a>");
                    Response.Write("</td>");                   
                    
                    
                    Response.Write("</tr>");
                    if (loopCount < bookings.Count() - 1)
                    {
                        Response.Write("<tr><td colspan=\"11\"><hr /></td></tr>");
                    }
                    loopCount++;
                }
            }
            else
            {
                Response.Write("<tr><td colspan=\"11\" align=\"center\">No active bookings present.</td></tr>");
            }           
            
        %>
        </table>
        </td></tr>
    </table>
    </td>
    </tr>

    <tr><td>
    <div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div>
    </td></tr>
    <tr>
    <td>
    
    </td>
    </tr>
    </table>
    </form>
</body>
</html>
